//
//  VipCardFormatViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/30.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VipCardFormatViewController.h"
#import "VipCardFormatView.h"
#import "SelectVipViewController.h"
#import "CompleteVipCardInfoViewController.h"

@interface VipCardFormatViewController ()

@property(nonatomic,weak)VipCardFormatView *vipCardFormatView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)NSMutableDictionary *dataDict;

@end

@implementation VipCardFormatViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.title = @"会员卡规格";
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateNormal];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    [self createSubView];
    
    [self loadData];
}

-(void)createSubView{
    
    
    VipCardFormatView *vipCardFormatView = [[VipCardFormatView alloc] init];
    
    vipCardFormatView.selectRow = self.selectRow;
    
    vipCardFormatView.cardType = self.commonCard.Type;
    
    //如果是新增的话
    if ([self.from isEqualToString:@"add"]) {
     
        vipCardFormatView.cardView.commonCard = self.commonCard;
        
        vipCardFormatView.cardView.selectRow = self.selectRow;
    }
    else{
        
        /**
         新开卡的话 有效期 会员 和 余额都是隐藏的，充值的情况才会出现
         */
        vipCardFormatView.validLabel.text = @"有效期:无";
        
        vipCardFormatView.vipNameLabel.text = @"无会员";
        
        vipCardFormatView.balanceDetailLabel.text = @"无";
    }
    
    vipCardFormatView.from = self.from;
    
    [vipCardFormatView.vipCardFormatDayView.selectVipBtnView.choseBtn addTarget:self action:@selector(selectVip)];
    
    [self.view addSubview:vipCardFormatView];
    
    self.vipCardFormatView = vipCardFormatView;
    
    [vipCardFormatView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
}


-(void)loadData{
    
    //Get_MemberCardSpecListDat
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
          
    [params setObject:self.commonCard.ID forKey:@"MemberCardID"];
    
    [params setObject:@"1" forKey:@"page"];
    
    [params setObject:@"100" forKey:@"rows"];
    
    [HTTPTool GET:Get_MemberCardSpecListData parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  self.dataSource = [NSMutableArray arrayWithArray:resultArray];
                  
                  self.vipCardFormatView.dataSource = self.dataSource;
              }
              
          }
     
          failure:^(NSError *error){                            
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}


#pragma mark 选择会员
-(void)selectVip{
    
    __weak typeof(self)weakSelf = self;
    
    SelectVipViewController *selectVipVC = [[SelectVipViewController alloc] init];
    
    NSString *memberId = [self.dataDict objectForKey:@"MemberID"];
    
    selectVipVC.selectVipIdStr = memberId;
    
    selectVipVC.sendSelectVipBlock = ^(NSString *userId, NSString *userName) {
    
        weakSelf.vipCardFormatView.vipCardFormatDayView.selectVipBtnView.choseLabel.text = userName;
        
        [weakSelf.dataDict setObject:userId forKey:@"MemberID"];
    };
    
    [self.navigationController pushViewController:selectVipVC animated:YES];
}

#pragma mark 下一步
-(void)rightBtnClick{
    
    //订单号
    NSString *orderNum = self.vipCardFormatView.vipCardFormatDayView.orderNumInputView.titleTF.text;
    
    //应付金额
    NSString *totalFee = self.vipCardFormatView.vipCardFormatDayView.copeWithInputView.titleTF.text;
    
    //实收金额
    NSString *factFee = self.vipCardFormatView.vipCardFormatDayView.receiptInputView.titleTF.text;
    
    if ([factFee isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入必填内容"];
        
        return;
    }
    
    if (self.vipCardFormatView.vipCardFormatDayView.receiptSwitchView.titleSwitch.isOn && [factFee isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入实收金额"];
        
        return;
    }
    
    //设置有效期是开的话
    if (self.vipCardFormatView.vipCardFormatDayView.setValidSwitchView && self.vipCardFormatView.vipCardFormatDayView.setValidSwitchView.titleSwitch.isOn) {
        
        if ([self.vipCardFormatView.vipCardFormatDayView.beginTimeStr isEmptyString] || [self.vipCardFormatView.vipCardFormatDayView.endTimeStr isEmptyString]) {
            
            [MBProgressHUD showText:@"请输入有效期"];
            
            return;
        }
        
    }
    
    //如果有开始时间的话 要设置开始时间
    if (self.vipCardFormatView.vipCardFormatDayView.beginTimeBtnView) {
        
        if ([self.vipCardFormatView.vipCardFormatDayView.beginTimeStr isEmptyString]) {
            
            [MBProgressHUD showText:@"请输入开始时间"];
            
            return;
        }
    }
    
    if (self.vipCardFormatView.vipCardFormatDayView.endTimeBtnView) {
        
        if ([self.vipCardFormatView.vipCardFormatDayView.endTimeStr isEmptyString]) {
            
            [MBProgressHUD showText:@"请输入结束时间"];
            
            return;
        }
    }
    
    
    //自动开卡
    [self.dataDict setObject:self.vipCardFormatView.vipCardFormatDayView.outoSwitchView.titleSwitch.isOn?@"1":@"0" forKey:@"IsAutoOpenCard"];
    
    [self.dataDict setObject:orderNum forKey:@"OrderNum"];
    
    [self.dataDict setObject:totalFee forKey:@"TotalFee"];
    
    [self.dataDict setObject:factFee forKey:@"FactFee"];
    
    //开始时间
    NSString *joinTime = self.vipCardFormatView.vipCardFormatDayView.beginTimeStr;
    
    //结束时间
    NSString *endTime = self.vipCardFormatView.vipCardFormatDayView.endTimeStr;
    
    //充值次数
    NSString *times = self.vipCardFormatView.vipCardFormatDayView.chargeTimeInputView?self.vipCardFormatView.vipCardFormatDayView.chargeTimeInputView.titleTF.text:@"";
    
    [self.dataDict setValue:joinTime forKey:@"JoinTime"];
    
    [self.dataDict setValue:endTime forKey:@"EndTime"];
    
    [self.dataDict setValue:times forKey:@"Times"];
    
    //获取当前选了哪个规格
    NSDictionary *cardSpec = nil;
    
    if (self.vipCardFormatView.currentSelect < self.dataSource.count) {
        
        cardSpec = [self.dataSource objectAtIndex:self.vipCardFormatView.currentSelect];
    }
    
    
    CompleteVipCardInfoViewController *completeVipCardVC = [[CompleteVipCardInfoViewController alloc] init];
    
    completeVipCardVC.selectRow = self.selectRow;
    
    completeVipCardVC.dataDict = self.dataDict;
    
    completeVipCardVC.cardSpec = cardSpec;
    
    completeVipCardVC.commonCard = self.commonCard;
    
    [self.navigationController pushViewController:completeVipCardVC animated:YES];
}


#pragma mark Get---

-(NSMutableDictionary *)dataDict{
    
    if (!_dataDict) {
        
        _dataDict = [[NSMutableDictionary alloc] init];
    }
    
    return _dataDict;
}

-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
        
    }
    
    return _dataSource;
}

@end
